package tree;


import java.util.*;

/**
 * @author: 叶子
 * @date: 2021/11/21 21:55
 **/
public class MultipleTree {
    public static void main(String[] args) {
//        读取层次数据结果列表
        List dataList = VirtualDataGenerator.getVirtualResult();
//  节点列表(散列表，用于临时存储节点对象)
        HashMap nodeList = new HashMap();
//  根节点
        Node root = null;
//        根据结果集构造节点列表(存入散列表)
        for (Iterator it = dataList.iterator(); it.hasNext(); ) {
            Map dataRecord = (Map) it.next();
            Node node = new Node();
            node.id = (String) dataRecord.get("id");
            node.text = (String) dataRecord.get("text");
            node.parentId = (String) dataRecord.get("parentId");
            nodeList.put(node.id, node);
        }
        Set entrySet = nodeList.entrySet();
        for (Iterator it = entrySet.iterator(); it.hasNext(); ) {
            Node node = (Node) ((Map.Entry) it.next()).getValue();
            if (node.parentId == null || node.parentId.equals("")) {
                root = node;
            } else {
                ((Node) nodeList.get(node.parentId)).addChild(node);
            }
        }
//        输出无序的树形菜单的JSON字符串
        System.out.println(root.toString());
//        对多叉树进行横向排序
        root.sortChildren();
//        输出有序的树形菜单的JSON字符串
        System.out.println(root.toString());
    }
}

class Node {
    public String id;
    public String text;
    public String parentId;
    private Children children = new Children();

    //    先序遍历，拼接json字符串
    @Override
    public String toString() {
        String result = "{" + "id:'" + id + "'" + ",text:'" + text + "'";
        if (children != null && children.getSize() != 0) {
            result += ",children:" + children.toString();
        } else {
            result += ",leaf : true";
        }
        return result + "}";
    }

    //    兄弟节点横向排序
    public void sortChildren() {
        if (children != null && children.getSize() != 0) {
            children.sortChildren();
        }
    }

    //    添加孩子节点
    public void addChild(Node node) {
        this.children.addChild(node);
    }
}

//  孩子列表类
class Children {
    private List list = new ArrayList();

    public int getSize() {
        return list.size();
    }

    public void addChild(Node node) {
        list.add(node);
    }

    //    拼接孩子节点的 JSON字符串
    @Override
    public String toString() {
        String result = "[";
        for (Iterator it = list.iterator(); it.hasNext(); ) {
            result += ((Node) it.next()).toString();
            result += ",";
        }
        result = result.substring(0, result.length() - 1);
        result += "]";
        return result;
    }

    //    孩子节点排序
    public void sortChildren() {
//        对本层节点进行排序
//        可根据不同的排序树形，传入不同的比较器，这里的传入id比较器
        Collections.sort(list, new NodeIDComparator());
//        对每个节点的下一层节点进行排序
        for (Iterator it = list.iterator(); it.hasNext(); ) {
            ((Node) it.next()).sortChildren();
        }
    }
}

class NodeIDComparator implements Comparator {
    //按照节点编号比较
    @Override
    public int compare(Object o1, Object o2) {
        int k1 = Integer.parseInt(((Node) o1).id);
        int k2 = Integer.parseInt(((Node) o2).id);
        return (k1 < k2 ? -1 : (k1 == k2 ? 0 : 1));
    }
}

class VirtualDataGenerator {
    //    构造无序的结果集列表，实际应用中，该数据应该从数据库中查询获得
    public static List getVirtualResult() {
        List dataList = new ArrayList();
        HashMap dataRecord1 = new HashMap();
        dataRecord1.put("id", "12000");
        dataRecord1.put("text", "成都市春熙路");
        dataRecord1.put("parentId", "10000");
        HashMap dataRecord2 = new HashMap();
        dataRecord2.put("id", "12200");
        dataRecord2.put("text", "简阳三岔湖");
        dataRecord2.put("parentId", "12000");
        HashMap dataRecord3 = new HashMap();
        dataRecord3.put("id", "12100");
        dataRecord3.put("text", "浦江石象湖");
        dataRecord3.put("parentId", "12000");
        HashMap dataRecord4 = new HashMap();
        dataRecord4.put("id", "13000");
        dataRecord4.put("text", "雅安碧峰峡");
        dataRecord4.put("parentId", "10000");
        HashMap dataRecord5 = new HashMap();
        dataRecord5.put("id", "00001");
        dataRecord5.put("text", "新都斑竹园");
        dataRecord5.put("parentId", "");
        HashMap dataRecord6 = new HashMap();
        dataRecord6.put("id", "10000");
        dataRecord6.put("text", "成都欢乐谷");
        dataRecord6.put("parentId", "00001");
        HashMap dataRecord7 = new HashMap();
        dataRecord7.put("id", "11000");
        dataRecord7.put("text", "成都动物园");
        dataRecord7.put("parentId", "10000");
        dataList.add(dataRecord1);
        dataList.add(dataRecord2);
        dataList.add(dataRecord3);
        dataList.add(dataRecord4);
        dataList.add(dataRecord5);
        dataList.add(dataRecord6);
        dataList.add(dataRecord7);
        return dataList;
    }
}